package kr.hkit.password.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;

import kr.hkit.password.MainActivity;
import kr.hkit.password.R;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.db.MyDatabaseManager;
import kr.hkit.password.dto.ChangeItem;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class AlarmService extends Service {
	boolean mQuit;
	private static final int ONEDAY = 60 * 60 * 24 * 1000;
	private static final String TAG = "PW";
	private NotificationManager mNotiManager;
	private AlarmThread thread;
	private Notification noti;
	private RemoteViews napView;
	private PendingIntent content;
	private MyDatabaseManager mIdb;
	private ArrayList<ChangeItem> cList;
	private InfoDao info;
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(TAG, "AlarmService onCreate()");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "AlarmService onDestroy()");
		mQuit = true;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "AlarmService onStartCommand()");
		mQuit = false;
		cList = new ArrayList<ChangeItem>();
		
		mNotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		intent = new Intent(AlarmService.this, MainActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		content = PendingIntent.getActivity(AlarmService.this, 0, intent, 0);

		napView = new RemoteViews(AlarmService.this.getPackageName(),
				R.layout.alarm_notiview);

		noti = new Notification.Builder(AlarmService.this)
				.setSmallIcon(R.drawable.ic_launcher)
				.setTicker("알람왔어요~")
				.setContentIntent(content)
				.setDefaults(Notification.DEFAULT_ALL)
				.setLights(0xff00ff00, 500, 500)
				.setAutoCancel(true).setContent(napView).build();

		noti.flags |= (Notification.FLAG_INSISTENT | Notification.FLAG_SHOW_LIGHTS);

		thread = new AlarmThread(this);
		thread.start();
		return START_STICKY;
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	class AlarmThread extends Thread {
		AlarmService mParent;

		public AlarmThread(AlarmService parent) {
			mParent = parent;
		}

		public void run() {
			while (!mQuit) {
				cList = new ArrayList<ChangeItem>();
				mIdb = MyDatabaseManager.getInstance(AlarmService.this);
				Cursor c = mIdb.select("list", mIdb.itemColumns, null, null, null, null, null);
				Log.d(TAG, "AlarmService c.getCount() = " + c.getCount());
				Calendar date = Calendar.getInstance();
				long checkDate = 0;
				while (c.moveToNext()) {
					try {
						date.setTime(InfoDao.SDF.parse(c.getString(5)));
					} catch (ParseException e) {e.printStackTrace();}
					
					switch (Integer.parseInt(c.getString(4))) {
					case 0:
						checkDate = date.getTimeInMillis() + 10*1000;
						break;
					case 1:
						checkDate = date.getTimeInMillis() + ONEDAY;
						break;
					case 2:
						checkDate = date.getTimeInMillis() + ONEDAY * 3;
						break;
					case 3:
						checkDate = date.getTimeInMillis() + ONEDAY * 7;
						break;
					case 4:
						checkDate = date.getTimeInMillis() + ONEDAY * 15;
						break;
					case 5:
						checkDate = date.getTimeInMillis() + ONEDAY * 30;
						break;
					}
					
					if (Calendar.getInstance().getTimeInMillis() - checkDate > 0) {
						mNotiManager.notify(cList.size(), noti);
						cList.add(new ChangeItem(c.getString(3), c.getString(5), c.getString(6)));
					}
				}
				if (getApplication() != null) {
					info = (InfoDao) getApplication();
					info.setChangeList(cList);
				}
				try {
					Thread.sleep(30 * 1000);
				} catch (Exception e) {
					Log.d(TAG, "run() catch!");
				}
			}
		}

	}
}
